<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="80px">
      <el-form-item label="订单状态" prop="orderState">
        <el-select v-model="queryParams.orderState" placeholder="请选择订单状态" clearable size="small">
          <el-option
            v-for="dict in orderStateOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="车牌号" prop="carNum">
        <el-input
          v-model="queryParams.carNum"
          placeholder="请输入车牌号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input
          v-model="queryParams.mobile"
          placeholder="请输入下单人手机号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="平台单号" prop="orderNum">
        <el-input
          v-model="queryParams.orderNum"
          placeholder="请输入订单号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="渠道单号" prop="outTradeNum">
        <el-input
          v-model="queryParams.outTradeNum"
          placeholder="请输入外部订单号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="订单类型" prop="orderType">
        <el-select v-model="queryParams.orderType" placeholder="请选择订单类型 1六年免检 2 代驾审车  3 自驾审车" clearable size="small">
          <el-option
            v-for="dict in orderTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="检测结果" prop="checkResult">
        <el-select v-model="queryParams.checkResult" placeholder="请选择检测结果  0未检测 1合格   2不合格" clearable size="small">
          <el-option
            v-for="dict in checkResultOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">

    </el-row>

    <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
      <el-table-column label="平台单号" align="center" prop="orderNum"/>
      <el-table-column label="渠道单号" align="center" prop="outTradeNum"/>
      <el-table-column label="手机号" align="center" prop="mobile"/>
      <el-table-column label="车牌号" align="center" prop="carNum"/>
      <el-table-column label="订单金额" align="center" prop="actPrice"/>
      <el-table-column label="代驾费" align="center" prop="driverFee"/>
      <el-table-column label="订单状态" align="center" prop="orderState" :formatter="orderStateFormat"/>
      <el-table-column label="订单类型" align="center" prop="orderType" :formatter="orderTypeFormat"/>
      <el-table-column label="检测结果" align="center" prop="checkResult" :formatter="checkResultFormat"/>
      <el-table-column label="预约时间" align="center" prop="bookTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.bookTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="检测站" align="center" prop="stationName"/>
      <el-table-column label="系统备注" align="center" prop="sysRemark"/>
      <el-table-column label="订单备注" align="center" prop="remark"/>
      <el-table-column label="取消原因" align="center" prop="cancelReason"/>
      <el-table-column label="接单时间" align="center" prop="driverPickTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.driverPickTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="finishTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.finishTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="gmtTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.gmtTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['third:order:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['third:order:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
  import { listOrder, getOrder, delOrder, addOrder, updateOrder, exportOrder } from '@/api/third/order'

  export default {
    name: 'Order',
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 总条数
        total: 0,
        // 订单表格数据
        orderList: [],
        // 弹出层标题
        title: '',
        // 是否显示弹出层
        open: false,
        // 订单状态字典

        orderStateOptions: [{
          dictLabel: '未支付',
          dictValue: '0'
        }, {
          dictLabel: '待接单',
          dictValue: '1'
        },
          {
            dictLabel: '已接单',
            dictValue: '3'
          },
          {
            dictLabel: '取车中',
            dictValue: '6'
          },
          {
            dictLabel: '已取车',
            dictValue: '7'
          },
          {
            dictLabel: '服务中',
            dictValue: '9'
          },
          {
            dictLabel: '检测中',
            dictValue: '12'
          },
          {
            dictLabel: '已检测',
            dictValue: '15'
          },
          {
            dictLabel: '送车中',
            dictValue: '18'
          }
          , {
            dictLabel: '已完成',
            dictValue: '21'
          },
          {
            dictLabel: '已取消',
            dictValue: '24'
          }],
        // 检测结果  0未检测 1合格   2不合格字典
        checkResultOptions:
          [{
            dictLabel: '未检测',
            dictValue: '0'
          }, {
            dictLabel: '合格',
            dictValue: '1'
          },
            {
              dictLabel: '不合格合格',
              dictValue: '2'
            }],
        // 检测结果  0未检测 1合格   2不合格字典
        orderTypeOptions:
          [{
            dictLabel: '六年免检',
            dictValue: '1'
          }, {
            dictLabel: '代驾审车',
            dictValue: '2'
          },
            {
              dictLabel: '自驾审车',
              dictValue: '3'
            }],
        // 查询参数
        queryParams:
          {
            pageNum: 1,
            pageSize: 10,
            appid: undefined,
            orderState:
            undefined,
            stationName: undefined,
            carNum: undefined,
            mobile: undefined,
            orderNum: undefined,
            outTradeNum: undefined,
            orderPrice: undefined,
            actPrice: undefined,
            stationFee: undefined,
            driverFee: undefined,
            orderType: undefined,
            checkResult: undefined,
            bookTime: undefined,
            sysRemark: undefined,
            cancelReason: undefined,
            expressNum: undefined,
            expressName: undefined,
            driverPickTime: undefined,
            finishTime: undefined,
            gmtTime: undefined
          }
        ,
        // 表单参数
        form: {}
        ,
        // 表单校验
        rules: {}
      }

    },
    created() {
      this.getList()
    },
    methods: {
      /** 查询订单列表 */
      getList() {
        this.loading = true
        listOrder(this.queryParams).then(response => {
          this.orderList = response.rows
          this.total = response.total
          this.loading = false
        })
      },
      // 订单状态字典翻译
      orderStateFormat(row, column) {
        return this.selectDictLabel(this.orderStateOptions, row.orderState)
      },
      // 订单状态字典翻译
      orderTypeFormat(row, column) {
        return this.selectDictLabel(this.orderTypeOptions, row.orderType)
      },
      // 检测结果  0未检测 1合格   2不合格字典翻译
      checkResultFormat(row, column) {
        return this.selectDictLabel(this.checkResultOptions, row.checkResult)
      },
      // 取消按钮
      cancel() {
        this.open = false
        this.reset()
      },
      // 表单重置
      reset() {
        this.form = {
          id: undefined,
          appid: undefined,
          orderState: undefined,
          stationName: undefined,
          carNum: undefined,
          mobile: undefined,
          remark: undefined,
          orderNum: undefined,
          outTradeNum: undefined,
          orderPrice: undefined,
          actPrice: undefined,
          stationFee: undefined,
          driverFee: undefined,
          orderType: undefined,
          checkResult: undefined,
          bookTime: undefined,
          sysRemark: undefined,
          cancelReason: undefined,
          expressNum: undefined,
          expressName: undefined,
          driverPickTime: undefined,
          finishTime: undefined,
          gmtTime: undefined
        }
        this.resetForm('form')
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1
        this.getList()
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm('queryForm')
        this.handleQuery()
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length != 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset()
        this.open = true
        this.title = '添加订单'
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset()
        const id = row.id || this.ids
        getOrder(id).then(response => {
          this.form = response.data
          this.open = true
          this.title = '修改订单'
        })
      },
      /** 提交按钮 */
      submitForm: function() {
        this.$refs['form'].validate(valid => {
          if (valid) {
            if (this.form.id != undefined) {
              updateOrder(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess('修改成功')
                  this.open = false
                  this.getList()
                } else {
                  this.msgError(response.msg)
                }
              })
            } else {
              addOrder(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess('新增成功')
                  this.open = false
                  this.getList()
                } else {
                  this.msgError(response.msg)
                }
              })
            }
          }
        })
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids
        this.$confirm('是否确认删除订单编号为"' + ids + '"的数据项?', '警告', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function() {
          return delOrder(ids)
        }).then(() => {
          this.getList()
          this.msgSuccess('删除成功')
        }).catch(function() {
        })
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams
        this.$confirm('是否确认导出所有订单数据项?', '警告', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function() {
          return exportOrder(queryParams)
        }).then(response => {
          this.download(response.msg)
        }).catch(function() {
        })
      }
    }
  }
</script>
